// utils/fetchRoutes.js
const { $fetchGov } = useNuxtApp()

export async function fetchRoutesFromAPI() {
    try {
      const { data } = await $fetchGov('/app/api/channel', {
        method: 'GET',
        params: {
            isNav: true,
            isIncludeChildren: true,
            isTree: true,
            offset: 0,
        }
      })
      
      return data.reduce((routes, channel) => {
        const base = `/${channel.routerPath}`
        routes.push(base)
        if (channel.children) {
          channel.children.forEach(child => {
            routes.push(`${base}/${child.routerPath}`)
          })
        }
        return routes
      }, [])
    } catch (error) {
      console.error('Failed to fetch routes:', error)
      return []
    }
  }